import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';


GlobalKey searchKey = GlobalKey();

class SearchBox extends StatefulWidget {
  final String hintText;
  final bool endDrawer;
  final TextEditingController searchController;
  final onSearch;

  SearchBox({
    Key key,
    this.hintText,
    this.endDrawer = false,
    this.searchController,
    this.onSearch
  }) 
  : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<SearchBox> {

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Expanded(
            flex: 1,
            child: Container(
              color: Color(0xFF297CFF),
              height: 90.w,
              padding: EdgeInsets.fromLTRB(0.w, 5.w, 20.w, 0),
              // decoration: BoxDecoration(border: Border.all(width: 1)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                      child: Row(
                      children: <Widget>[
                        InkWell(
                          onTap: () {
                            Navigator.pop(context);
                          },
                          child: Container(
                            width: 64.w,
                            height: 64.w,
                            child: Align(
                              child: Image.asset('assets/arrow_return.png', width: 17.w,),
                            )
                          ),
                        ),
                        Container(
                          width: widget.endDrawer ? 640.w : 540.w,
                          height: 64.w,
                          margin: EdgeInsets.only(left: 13.w),
                          child: TextField(
                            onSubmitted: (value) =>  {
                              setState(() {
                                widget.onSearch(value);
                              })
                            },
                            controller: widget.searchController,
                            textInputAction: TextInputAction.search,
                            cursorColor: Colors.white,
                            style: TextStyle(
                              fontSize: 14, color: Colors.white
                            ),//输入文本的样式
                            decoration: InputDecoration(
                              prefixIcon: Container(
                                padding: EdgeInsets.all(20.w),
                                child: Image.asset('assets/search.png'),
                              ),
                              hintText: widget.hintText,
                              hintStyle: TextStyle(color: Color(0xFF93B9F5), fontSize: 14),
                              contentPadding: EdgeInsets.fromLTRB(77.w, 0, 0, 15),
                              fillColor: Color(0XFF1064E9),
                              filled: true,
                              enabledBorder: OutlineInputBorder(
                                borderRadius: BorderRadius.all(
                                  Radius.circular(55.w), 
                                ),
                                borderSide: BorderSide(
                                  color: Colors.transparent, 
                                  width: 0, 
                                ),
                              ),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(
                                  color: Colors.transparent, 
                                  width: 0, 
                                ),
                                borderRadius: BorderRadius.all(
                                  Radius.circular(55.w), 
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Offstage(
                    offstage: widget.endDrawer,
                    child: Builder(
                      builder: (BuildContext context){
                        return InkWell(
                          onTap: () {
                            Scaffold.of(context).openEndDrawer();
                          },
                          child: Row(
                            children: <Widget>[
                              Padding(
                                padding: EdgeInsets.only(right: 10.w),
                                child: Align(
                                  alignment: Alignment(0,-0.1),
                                  child: Text('筛选', style: TextStyle(
                                    color: Colors.white, fontSize: 14
                                  ),),
                                ),
                              ),
                              Image.asset('assets/sx.png', width: 22.w)
                            ]
                          )
                        );
                      }
                    ),
                  )
                ]
              )
            )
          )
        ]
      )
    );
  }
}